package com.msos.servlet.mainPage;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.msos.model.customer.Customer;
import com.msos.model.manager.Manager;
import com.msos.service.customerService.CustomerService;
import com.msos.utils.CommonsUtils;
import com.msos.model.respBean.RespBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Properties;

/**
 * 弟中弟：小邦
 */

//主页面
@WebServlet(urlPatterns = "/index")
public class MainPageServlet extends HttpServlet {
    private static final Properties SYSTEM_INFO = new Properties();
    CustomerService customerService = new CustomerService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if("quit".equals(action)){
            //退出登录，并返回登录页面
            HttpSession session = req.getSession();
            session.invalidate();
            resp.sendRedirect("/MSOS/login");
        }else if("update".equals(action)){
            req.getRequestDispatcher("/WEB-INF/jsp/admin/update.jsp").forward(req,resp);
        }
        else {
            String uname = "";
            Customer customer = (Customer)req.getSession().getAttribute("loginCustomer");
            String username = customer.getUsername();
            String gender = customer.getGender();
            if (gender.equals("男")){
                uname = "["+username+"]先生  您好！";
            }else {
                uname = "["+username+"]女士  您好！";
            }

            SYSTEM_INFO.load(MainPageServlet.class.getResourceAsStream("/system_info.properties"));
            SYSTEM_INFO.setProperty("schoolName",uname);
            req.setAttribute("si",SYSTEM_INFO);
            //展示主页面
            req.getRequestDispatcher("/WEB-INF/jsp/admin/admin.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        String newPassword1 = req.getParameter("newPassword1");
        String newPassword2 = req.getParameter("newPassword2");
        Integer rs = customerService.updateCustomerByPassword(phone, password, newPassword1, newPassword2,req);
        RespBean respBean = null;
        if(rs == CommonsUtils.UPDATE_PASSWORD_SUCCESS){
            respBean = RespBean.ok("修改成功");
        }else if(rs == CommonsUtils.UPDATE_PASSWORD_ERROR){
            respBean = RespBean.error("新密码前后不一致");
        }else if(rs == CommonsUtils.USER_BLOCKED){
            respBean = RespBean.error("用户被封禁");
        }else if(rs == CommonsUtils.USER_NOTFOUND_OR_PASSWORD_BAD){
            respBean = RespBean.error("账户或者密码错误");
        }else {
            respBean = RespBean.error("未知错误");
        }
        resp.setContentType("application/json;charset=utf-8");
        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(respBean);
        resp.getWriter().write(json);
    }
}
